package com.atguigu.aopD;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Author: liuaojun
 * @CreateTime: 2025-01-16
 * @Description:
 * @Version: 1.0
 */
public class CaclImplDynamicProxy {
    private Object cacl;
    public CaclImplDynamicProxy(Object cacl) {
        this.cacl = cacl;
    }
    public Object getCacl() {
        Object proxy=null;
        ClassLoader classLoader = cacl.getClass().getClassLoader();
        Class<?>[] interfaces = cacl.getClass().getInterfaces();
        proxy=Proxy.newProxyInstance(classLoader, interfaces, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                String name = method.getName();
                MyLogging.methodBefore(name,args);
                Object rs = method.invoke(cacl, args);
                MyLogging.methodAfter(name,rs);
                return rs;
            }
        });
        return proxy;
    }

}
